<?PHP
session_start();
include "url.php";
include_once "ini.php";
$id_cam = $_POST['id_cam'];
$time = $_POST['time'];
$sol = $_POST['sol'];
$timestamp = date("Y-m-d H:i:s");
$timestamp = time($timestamp);
dbconnect();
$sql = "SELECT * FROM statistiques WHERE id_user = '".$id_cam."';";
$requete = mysql_query($sql);
if (!$requete)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{
    $tab = mysql_fetch_assoc($requete);
    $sols = $tab['sols'];
    echo $sols, "\r";
    $nb_visit = $tab['nb_visit'];
    $somme_duree = $tab['somme_duree'];
    $start_date = time($tab['start_dat']);
    $sols = $sols+$sol;
    $nb_visit++;
    $somme_duree += $time;
    $duree = $somme_duree/$nb_visit;
    $delta_time = $timestamp-$time;//date de début de la visite (nowdate - temps de visite)
    $periode = $start_date-$delta_time;
    $periode = $periode/$nb_visit;
    $delta_time = date("Y-m-d H:i:s", $delta_time);
    $query = "UPDATE `statistiques` SET `sols` = '".$sols."', nb_visit = '".$nb_visit."', duree_visit = '".$duree."', periode = '".$periode."', somme_duree = '".$somme_duree."', last_visit = '".$delta_time."' WHERE `statistiques`.`id_user` = '".$id_cam."' LIMIT 1 ;";
    $result = mysql_query($query);
    if (!$result)
    {
        echo "SQL error : ", $query;
        exit ;
    }
    else
    {
        echo "save statistques done.";
    }
}
$sql = "SELECT * FROM statistiques WHERE id_user = '".$_SESSION['id_user']."';";
$requete = mysql_query($sql);
if (!$requete)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{
    $tab = mysql_fetch_assoc($requete);
    $sols = $tab['sols'];
    echo $sols, "\r";
    $sols = $sols-$sol;
    
    $query = "UPDATE `statistiques` SET `sols` = '".$sols."' WHERE `statistiques`.`id_user` = '".$_SESSION['id_user']."' LIMIT 1 ;";
    $result = mysql_query($query);
    if (!$result)
    {
        echo "SQL error : ", $query;
        exit ;
    }
    else
    {
        echo "save statistques done.";
    }
}

$sql = "SELECT * FROM statistiques ORDER BY nb_visit DESC LIMIT 10;";
$requete = mysql_query($sql);
if (!$requete)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{
    $top_ten = "";
    while ($tab = mysql_fetch_assoc($requete))
    {
        $top_ten .= $tab['id_user'].":";
    }
    $query = "UPDATE `hotspot` SET `top_ten` = '".$top_ten."';";
    $result = mysql_query($query);
    if (!$result)
    {
        echo "SQL error : ", $query;
        exit ;
    }
    else
    {
        echo "top ten done";
    }
}

$sql = "SELECT * FROM statistiques ORDER BY nb_visit ASC LIMIT 10;";
$requete = mysql_query($sql);
if (!$requete)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{
    $bottom_ten = "";
    while ($tab = mysql_fetch_assoc($requete))
    {
        $bottom_ten .= $tab['id_user'].":";
    }
    $query = "UPDATE `hotspot` SET `bottom_ten` = '".$bottom_ten."';";
    $result = mysql_query($query);
    if (!$result)
    {
        echo "SQL error : ", $query;
        exit ;
    }
    else
    {
        echo "bottom ten done";
    }
}

$sql = "SELECT * FROM statistiques ORDER BY last_visit DESC LIMIT 10;";
$requete = mysql_query($sql);
if (!$requete)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{
    $last_ten = "";
    while ($tab = mysql_fetch_assoc($requete))
    {
        $last_ten .= $tab['id_user'].":";
    }
     $query = "UPDATE `hotspot` SET `last_ten` = '".$last_ten."';";
    $result = mysql_query($query);
    if (!$result)
    {
        echo "SQL error : ", $query;
        exit ;
    }
    else
    {
        echo "last ten done";
    }
}


$sql = "SELECT * FROM statistiques WHERE id_user = '".$_SESSION['id_user']."';";
$requete = mysql_query($sql);
if (!$requete)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{
    $tab = mysql_fetch_assoc($requete);
    $sols = $tab['sols'];
	$sols = $sols-$sol;
	 $query = "UPDATE `statistiques` SET `sols` = '".$sols."' WHERE id_user = '".$_SESSION['id_user']."';";
	 $result = mysql_query($query);
    if (!$result)
    {
        echo "SQL error : ", $query;
        exit ;
    }
    else
    {
        echo "save my sol done.";
    }
}
dbclose();
?>
